package com.project.bohang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.project.bohang.common.exception.BusinessException;
import com.project.bohang.common.model.Response;
import com.project.bohang.common.model.page.PageResult;
import com.project.bohang.entity.House;
import com.project.bohang.pojo.dto.HouseDto;
import com.project.bohang.pojo.vo.HouseVo;
import com.project.bohang.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.project.bohang.controller.BaseController;

import java.util.List;

/**
 *  房屋管理 （管理员）
 *
 * @author bohang
 * @since 2022-03-26
 */
@Controller
@RequestMapping("/house")
@ResponseBody
public class HouseController extends BaseController {

    @Autowired
    private HouseService houseService;

    /**
     * 房屋列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public Response<PageResult<HouseVo>> selectList(@RequestBody HouseDto dto){
        return Response.success(getDataTable(houseService.selectList(dto)));
    }

    /**
     * 新增房屋信息
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody House req) {
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("name", req.getName());
        wrapper.eq("deleted", 0);
        House house = houseService.getOne(wrapper);
        if (house != null) {
            throw new BusinessException("500003", "该住户在已存在");
        }
        houseService.save(req);
        return Response.success();
    }


    /**
     * 修改房屋信息
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody House req) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",req.getName());
        House house =  houseService.getOne(queryWrapper);
        if(!house.getName().equals(req.getName())){
            throw new BusinessException("500005","门牌号已存在");
        }
        UpdateWrapper<House> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        houseService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除房屋记录
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody House req) {
        House entity = new House();
        entity.setId(req.getId());
        entity.setDeleted("1");
        houseService.updateById(entity);
        return Response.success();
    }

    /**
     * 房屋信息详情
     *
     * @param req
     * @return
     */
    @PostMapping("/detail")
    public Response<House> detail(@RequestBody House req) {
        House Visit = houseService.getById(req.getId());
        return Response.success(Visit);
    }
}

